﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.Micro;
using System.Collections.ObjectModel;
using MoneyManagerNET.Model;
using System.Collections.Specialized;
using System.Windows.Input;
using System.ComponentModel.Composition;
using CaliburnWpf.ViewModels.Infrastructure;

namespace CaliburnWpf.ViewModels
{
    [Export(typeof(IShell))]
    public class ShellViewModel : Conductor<IScreen>.Collection.OneActive, IConductActiveItem
    {
        IWindowManager _windowManager;

        public IWindowManager windowManager
        {
            get { if (_windowManager == null)_windowManager = new WindowManager(); return _windowManager; }
            set { _windowManager = value; }
        }


        [ImportingConstructor]
        public ShellViewModel(IWindowManager windowManager)
        {
            this.windowManager = windowManager;
        }

        public ShellViewModel()
        { 
            //_bankAccounts = new ObservableCollection<IBankAccount>(
            //    App.Storage.Select<IBankAccount>()
            //    );

            //_stockAccounts = new ObservableCollection<IStockAccount>(
            //    App.Storage.Select<IStockAccount>().ToList()
            //    );

            //this.viewModels = new ViewModelBase[] { new HomeViewModel(), this.BankAccountViewModel, new StockAccountViewModel(), new AssetListViewModel() };
        }
       
        

        public void CreateAccount()
        {
            //var vm=(new CreateAccountViewModel() { DisplayName = "Create account" });

            //var r = vm.ShowDialog();
            //if (r.HasValue && r.Value)
            //{
            //    var account = vm.CreateAccount();

            //    var vm2 = new EditAccountViewModel() { Model = account };
            //    var r2 = vm2.ShowDialog();
            //    if (r2.HasValue && r2.Value)
            //    {
            //        if (account is IBankAccount)
            //            _bankAccounts.Add(vm2.ModelAs as IBankAccount);

            //        if (account is IStockAccount)
            //            _stockAccounts.Add(vm2.ModelAs as IStockAccount);

            //        App.Storage.SaveAll();
            //    }
            //}
        }

        public void ManageContragents()
        {
            var vm = new Dictionaries.ContragentListViewModel();
            windowManager.ShowDialog(vm);
            App.Storage.SaveAll();
        }

        public void ManageCategories()
        {
            var vm = new Dictionaries.CategoryListViewModel();
            windowManager.ShowWindow(vm);
            App.Storage.SaveAll();
        }

        public void RelocateByCategory()
        {
            //var vm = new RelocateCategoryViewModel();
            //var r = vm.ShowDialog();
            //if (r.HasValue && r.Value)
            //{
            //    App.Service.RelocateByCategory(vm.FromCategory, vm.FromSubCategory, vm.ToCategory, vm.ToSubCategory, null);
            //    App.Storage.SaveAll();

            //}

        }

        public void RelocateByContargent()
        {
            //var vm = new RelocateContragentViewModel();
            //var r = vm.ShowDialog();
            //if (r.HasValue && r.Value)
            //{
            //    App.Service.RelocateByContragent(vm.FromContragent, vm.ToContragent, null);
            //    App.Storage.SaveAll();

            //}
        }


        protected override void OnDeactivate(bool close)
        {
            App.Storage.SaveAll();

            base.OnDeactivate(close);
        }


    }
}
